package v3.object.space;

import galaxy.obj.Point;

import java.text.DecimalFormat;
import java.util.Random;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

import v3.object.ship.weapon.iWeapon;

public abstract class aSpaceObj implements iSpaceObj
{
	protected Point position;
	protected SpaceObjType spaceObjType;
	protected long objId = -1;
	protected Point sectorPosition=null;
	protected DecimalFormat format_decimal = new DecimalFormat("###,###.##");
	protected DecimalFormat format_00 = new DecimalFormat("00");
	protected Random rnd = new Random();
	protected double shield;
	protected double hull;
	protected boolean alive=true;
	
	@Override
	public SpaceObjType getType()
	{
		return spaceObjType;
	}

	@Override
	public long getId()
	{
		return objId;
	}	

	@Override
	public Point getPosition()
	{
		return position;
	}

	@Override
	public void setPosition(Point position)
	{
		this.position=position;		
	}
	
	
	@Override	
	public Point getSectorPosition()
	{
		return sectorPosition;
	}
	

	public void setSectorPosition(Point sectorPosition)
	{
		this.sectorPosition = sectorPosition;
	}

	@Override
	public Point getQuadrant()
	{
		Point q = new Point(Math.floor(position.getX()/10),Math.floor(position.getY()/10),Math.floor(position.getZ()/10));
		return q;
	}
	
	public String getInfo()
	{
		return toString();
	}
	
	abstract  public void collision(iSpaceObj o);
	
	
	public void HitByWeapon(iWeapon w)
	{
		if (shield>0)
		{
			shield=shield-rnd.nextDouble()*w.getPowerAgainstShieldMult()-w.getPowerAgainstShieldAdd();
			if(shield<0)
			{
				hull=hull+shield; // damage that overflow Shield affect hull
				shield=0;
			}
		}
		else
		{
			hull=hull-rnd.nextDouble()*w.getPowerAgainstHulldMult()-w.getPowerAgainstHulldAdd();
		}
	}

	@Override
	public String toString()
	{
		return (position != null ? position + ", " : "") + (spaceObjType != null ?  spaceObjType + ", " : "") + "objId=" + objId + ", "
				+ (sectorPosition != null ? "positionManover=" + sectorPosition : "") + "]";
	}

	public boolean isAlive()
	{
		alive=hull<=0 && shield<=0;
		return alive;
	}

	public void setAlive(boolean isAlive)
	{
		this.alive = isAlive;
	}

	public void generate(Universe u) throws Exception
	{
		position = u.getRandomPosition(); 
		objId=System.currentTimeMillis();
	}
}
